#include <multiboot.h>
#include <init/dt.h>
#include <hw/timer.h>
#include <hw/keyboard.h>
#include <hw/console.h>
#include <utils/debug.h>
#include <mm/kmalloc.h>

void kmain(multiboot_info_t *mbi, uint32 magic ) {
    console_init();
    kprintf("Validating multiboot magic number...");
    if (magic != MULTIBOOT_BOOTLOADER_MAGIC) {
        kprintf ("Invalid multiboot magic number: 0x%x\n", magic);
        return;
    }
    kprintf("done\n");
    kprintf("Boot loader is %s\n", (char*)mbi->boot_loader_name);
    kprintf("Initializing descriptor tables...");
    dt_init();
    kprintf("done\n");
    kprintf("Starting system timer...");
    timer_init();
    kprintf("done\n");
    kprintf("Current time is...%d\n", time());
    kprintf("Starting keyboard with default mapping...");
    keyboard_init();
    kprintf("done\n");
    kprintf("Detecting memory...");
    uint32 total_mem = 0x1000000; // default 16MB
    if(CHECK_FLAG(mbi->flags, 0)) {
        kprintf(" (bootloader) ");
        total_mem = mbi->mem_upper * 1024;
    }
    kprintf("%d\n", total_mem);
    kprintf("Initializing memory manager...");
    malloc_init(total_mem);
    kprintf("done\n");
    asm volatile("sti");
    for(;;) {
    }
}
